package cn.com.nthotline.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.com.nthotline.R;

public class WebViewActivity extends Activity {
	private ImageButton back;
	private TextView textTitle;
	private static String url;
	private static String title;
	private WebView webView;
	private ProgressBar progress;
	private static final int ERROR=0;	
	public static void setUrl(String _url){
		url=_url;
	}
	public static void setTitle(String _title){
		title=_title;
	}
	private Handler handler=new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch (msg.what) {
			case ERROR:
//					String str=(String)msg.obj;
//					Toast.makeText(getBaseContext(), str, 1).show();
				break;

			default:
				break;
			}
		}
		
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_webview);
		init();
		registerListener();
	}

	private void init() {
		back=(ImageButton)findViewById(R.id.action_bar_button_back);
		textTitle=(TextView)findViewById(R.id.action_bar_textview_title);
		progress=(ProgressBar)findViewById(R.id.web_pb);
		textTitle.setText("");
		textTitle.setText(title);
		webView = (WebView)findViewById(R.id.web_webview);
		progress=(ProgressBar)findViewById(R.id.web_pb);
		webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		WebSettings settings = webView.getSettings();
		settings.setSupportZoom(true);
		settings.setBuiltInZoomControls(true);
		settings.setJavaScriptEnabled(true);
		webView.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, final int newProgress) {
				// TODO Auto-generated method stub
				handler.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						if (100 == newProgress) {
							progress.setVisibility(View.GONE);
						} else {
							progress.setProgress(newProgress);
						}
					}
				});
			}

		});
		webView.setWebViewClient(new WebViewClient() {

			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				// TODO Auto-generated method stub
					super.onReceivedError(view, errorCode, description, failingUrl);
					handler.obtainMessage(ERROR, description).sendToTarget();
			}

			@Override
			public void onPageFinished(WebView view, String url) {
				// TODO Auto-generated method stub
				super.onPageFinished(view, url);
				progress.setVisibility(View.GONE);
			}

			@Override
			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				// TODO Auto-generated method stub
				super.onPageStarted(view, url, favicon);
			}

		});
		webView.loadUrl(url);
		
		
		
	}

	private void registerListener() {
		back.setOnClickListener(clickListener);
	}
	private OnClickListener clickListener=new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.action_bar_button_back:
				finish();
				break;
			
			default:
				break;
			}
		}
	};
}
